package com.itheima.dubbo.api;

import com.itheima.domain.mongo.*;
import com.itheima.domain.vo.PageResult;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 阿鹏
 * @date 2021/2/3 22:11
 */
@Service
public class UserLikeApiImpl implements UserLikeApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public long queryLikeCount(long userId) {
        Query query = Query.query(Criteria.where("userId").is(userId));
        return mongoTemplate.count(query, UserLike.class);
    }

    @Override
    public long queryFansCount(long userId) {
        Query query = Query.query(Criteria.where("likeUserId").is(userId));
        return mongoTemplate.count(query, UserLike.class);
    }

    @Override
    public long queryFriendsCount(long userId) {
        Query query = Query.query(Criteria.where("userId").is(userId));
        return mongoTemplate.count(query, Friend.class);
    }

    @Override
    public PageResult<CommonLikeUser> queryFriendsList(Long userId, int page, int pagesize) {
        Query query = Query.query(Criteria.where("userId").is(userId));
        List<Friend> friendList = mongoTemplate.find(query, Friend.class);
        long count = mongoTemplate.count(query, Friend.class);
        List<CommonLikeUser> likeUserList = new ArrayList<>();
        for (Friend friend : friendList) {
            CommonLikeUser likeUser = new CommonLikeUser();
            likeUser.setUserId(friend.getUserId());
            likeUser.setTargetUserId(friend.getFriendId());
            likeUser.setScore(getScore(friend.getUserId(), friend.getFriendId()));
            likeUserList.add(likeUser);
        }
        int pages = (int) Math.ceil(count * 1.0 / pagesize);
        return new PageResult<>((int) count, pagesize, pages, page, likeUserList);
    }

    @Override
    public PageResult<CommonLikeUser> queryLikeList(Long userId, int page, int pagesize) {
        Query query = Query.query(Criteria.where("userId").is(userId));
        List<UserLike> userLikeList = mongoTemplate.find(query, UserLike.class);
        long count = mongoTemplate.count(query, UserLike.class);
        List<CommonLikeUser> likeUserList = new ArrayList<>();
        for (UserLike userLike : userLikeList) {
            CommonLikeUser likeUser = new CommonLikeUser();
            likeUser.setUserId(userLike.getUserId());
            likeUser.setTargetUserId(userLike.getLikeUserId());
            likeUser.setScore(getScore(userLike.getUserId(), userLike.getLikeUserId()));
            likeUserList.add(likeUser);
        }
        int pages = (int) Math.ceil(count * 1.0 / pagesize);

        return new PageResult<>((int) count, pagesize, pages, page, likeUserList);
    }

    @Override
    public PageResult<CommonLikeUser> queryFansList(Long userId, int page, int pagesize) {
        Query query = Query.query(Criteria.where("likeUserId").is(userId));
        List<UserLike> userLikeList = mongoTemplate.find(query, UserLike.class);
        long count = mongoTemplate.count(query, UserLike.class);
        List<CommonLikeUser> commonLikeUserList = new ArrayList<>();
        for (UserLike userLike : userLikeList) {
            CommonLikeUser commonLikeUser = new CommonLikeUser();
            commonLikeUser.setUserId(userLike.getLikeUserId());
            commonLikeUser.setTargetUserId(userLike.getUserId());
            commonLikeUser.setScore(getScore(userLike.getUserId(), userLike.getLikeUserId()));
            commonLikeUserList.add(commonLikeUser);
        }

        int pages = (int) Math.ceil(count * 1.0 / pagesize);
        return new PageResult<>((int)count, pagesize, pages, page, commonLikeUserList);
    }

    @Override
    public PageResult<CommonLikeUser> findVisitorList(Long userId, int page, int pagesize) {
        Query query = Query.query(Criteria.where("userId").is(userId))
                .with(Sort.by(Sort.Order.desc("date")))
                .with(PageRequest.of(page - 1, pagesize));
        List<Visitor> visitorList = mongoTemplate.find(query, Visitor.class);
        long count = mongoTemplate.count(query, Visitor.class);
        List<CommonLikeUser> userList = new ArrayList<>();
        for (Visitor visitor : visitorList) {
            CommonLikeUser user = new CommonLikeUser();
            user.setUserId(visitor.getUserId());
            user.setTargetUserId(visitor.getVisitorUserId());
            user.setScore(getScore(visitor.getUserId(), visitor.getVisitorUserId()));
            userList.add(user);
        }

        int pages = (int) Math.ceil(count * 1.0 / pagesize);
        return new PageResult<>((int) count, pagesize, pages, page, userList);
    }

    @Override
    public void delete(Long userId, Long likeUserId) {
        Query query = Query.query(Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId));
        mongoTemplate.remove(query, UserLike.class);
    }

    public Double getScore(long userId, long toUserId) {
        Query query = Query.query(Criteria.where("userId").is(userId).and("toUserId").is(toUserId));
        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);
        if (recommendUser != null) {
            return recommendUser.getScore();
        }
        return 95D;
    }

    /**
     *
     * @param userId 当前用户
     * @param likeUserId 喜欢的用户id
     */
    @Override
    public void saveLikeUser(Long userId, Long likeUserId) {
        UserLike userLike = new UserLike();
        userLike.setId(ObjectId.get());
        userLike.setUserId(userId);
        userLike.setLikeUserId(likeUserId);
        userLike.setCreated(System.currentTimeMillis());
        mongoTemplate.save(userLike);
    }

    @Override
    public boolean findIfLikeMe(Long userId, Long likeUserId) {
        Query query = new Query(Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId));
        boolean exists = mongoTemplate.exists(query, UserLike.class);
        return exists;
    }

    @Override
    public void removeLike(Long userId, Long targetUserId) {
        Query query = new Query(Criteria.where("userId").is(userId).and("likeUserId").is(targetUserId));
        mongoTemplate.remove(query, UserLike.class);
    }
}
